import java.util.ArrayList;

/**
 * Created by Administrator on 2016/5/11.
 */

// 气象站数据类，属于主题
public class WeatherData implements Subject {

    private ArrayList observers;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observers = new ArrayList();
    }

    public void registerObserver(Observer o) {

        observers.add(o);
    }

    public void removeObserver(Observer o) {

        int i = observers.indexOf(o);
        if (i >= 0) {
            observers.remove(i);
        }
    }

    public void notifyObservers() {

        for (int i = 0; i < observers.size(); i++) {

            Observer observer = (Observer) observers.get(i);
            observer.update(temperature, humidity, pressure);
        }
    }

    public void measurementChanged() {

        notifyObservers(); // 当观测值变化时，通知所有观察者
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {

        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;

        // 先从气象站获取新数据，再通知观察者
        measurementChanged();
    }

}
